﻿using System;
using System.Windows.Forms;
using TimeTable.Domain.DataAccess;
using TimeTable.Domain.Entity;

namespace TimeTable.AddForms
{
    public partial class NewTimeTableForm : Form
    {
        public NewTimeTableForm()
        {
            InitializeComponent();

            faculty.DataSource = DataAccessConnection.Instance.GetDivisionsByType(DivisionType.Faculty);

            beginDate.Value = new DateTime(DateTime.Now.Year, 9, 1, 8, 0, 0);
            endDate.Value = new DateTime(DateTime.Now.Year, 12, 31, 18, 0, 0);
        }

        private void CancelButtonClick(object sender, EventArgs e)
        {
            Close();
        }

        private void OKButtonClick(object sender, EventArgs e)
        {
            try
            {
                var division = faculty.SelectedItem as Division;
                if (division == null) throw new Exception("Please select faculty!");

                if (beginDate.Value >= endDate.Value) throw new Exception("Please choose correct date!");

                var timeTable = new Domain.Entity.TimeTable
                {
                    Division = division,
                    BeginDate = beginDate.Value,
                    EndDate = endDate.Value
                };

                if (DataAccessConnection.Instance.TimeTableExists(timeTable))
                {
                    MessageBox.Show("Time table already exists!");
                }
                else
                {
                    DataAccessConnection.Instance.AddTimeTable(timeTable);

                    MessageBox.Show("Time table successfully added!");

                    Close(); 
                }
            }
            catch(Exception exception)
            {
                MessageBox.Show(exception.Message, "Error");
            }
        }
    }
}
